package api

import (
	"github.com/gin-gonic/gin"
	"guard/internal/app/oauth/serivce"
	"guard/internal/app/oauth/store"
	"guard/internal/db"
	"guard/internal/middleware"
)

var oauthService service.OAuthService

func init() {
	oauthService = service.NewOAuthService(store.NewOAuthStore(db.NewMySqlStore()))
}

func InitOAuthRouter(router *gin.Engine) {

	// oauth2相关的
	oauth := router.Group("oauth2")
	{
		// 错误直接重定向，需要登录
		authorizeReq := oauth.Group("authorize", middleware.AuthLogin, middleware.ErrHtmlHandler("error.html"))
		{
			authorizeReq.GET("", authorize)
			authorizeReq.POST("", authorize)
		}
		// 错误返回json，也不需要登录，需要跨域
		tokenReq := oauth.Group("", middleware.Cors, middleware.ErrJsonHandler)
		{
			tokenReq.POST("token", token)
			tokenReq.POST("revoke", revoke)
		}
	}
}
